/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package InterfaceGrafica;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import mercadoria.Mercadoria;
import trabalho4paradigmas.Controle;

/**
 *
 * @author Luis Henrique
 */
public class JanelaVenda extends javax.swing.JFrame {

    /**
     * Creates new form JanelaVenda
     */
    private String qtdProduto, nomeProduto, nomeCliente;
    private Controle carregaControle;
    private DefaultListModel listaProdutos;
    ArrayList<Mercadoria> produtosVendidos = new ArrayList<Mercadoria>(); 
    public JanelaVenda() throws IOException {
        initComponents();
        carregaControle= new Controle();   
        carregaComboBox();
    }
      
    public JanelaVenda(Controle c){
        carregaControle= c;
        initComponents();
        carregaComboBox();
        carregaComboBox2();
        carregaJList(); 
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextVendaQuantidade = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListProdutos = new javax.swing.JList();
        jButtonAdicionaLista = new javax.swing.JToggleButton();
        jButtonVendaFinaliza = new javax.swing.JToggleButton();
        jComboNomeClientes = new javax.swing.JComboBox();
        jComboProdutos = new javax.swing.JComboBox();
        jTextValtotal = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Adobe Hebrew", 0, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("REGISTRA VENDA");

        jLabel2.setText("Selecione o Nome do Cliente");

        jLabel4.setText("Selecione o Produto");

        jLabel5.setText("Digite a quantidade");

        jScrollPane1.setViewportView(jListProdutos);

        jButtonAdicionaLista.setFont(new java.awt.Font("Adobe Hebrew", 0, 18)); // NOI18N
        jButtonAdicionaLista.setText("Adicionar a Lista");
        jButtonAdicionaLista.setToolTipText("");
        jButtonAdicionaLista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAdicionaListaActionPerformed(evt);
            }
        });

        jButtonVendaFinaliza.setFont(new java.awt.Font("Adobe Hebrew", 0, 18)); // NOI18N
        jButtonVendaFinaliza.setText("Finalizar Venda");
        jButtonVendaFinaliza.setAutoscrolls(true);
        jButtonVendaFinaliza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVendaFinalizaActionPerformed(evt);
            }
        });

        jComboNomeClientes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboNomeClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboNomeClientesActionPerformed(evt);
            }
        });

        jComboProdutos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(55, 55, 55)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(55, 55, 55)
                            .addComponent(jLabel2))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jComboNomeClientes, 0, 281, Short.MAX_VALUE)
                                .addComponent(jComboProdutos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jLabel5))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextVendaQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonAdicionaLista)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                        .addComponent(jButtonVendaFinaliza))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextValtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(46, 46, 46)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jComboNomeClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jComboProdutos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(jTextVendaQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jTextValtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonAdicionaLista)
                            .addComponent(jButtonVendaFinaliza))))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonVendaFinalizaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVendaFinalizaActionPerformed
        // TODO add your handling code here:
        nomeCliente=(String) jComboNomeClientes.getSelectedItem();
        carregaControle.Vendas(nomeCliente, produtosVendidos);
        try {
            //jTextValtotal.setText();
            carregaControle.grava();
        } catch (IOException ex) {
            Logger.getLogger(JanelaVenda.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(false);
    }//GEN-LAST:event_jButtonVendaFinalizaActionPerformed

    private void jComboNomeClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboNomeClientesActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jComboNomeClientesActionPerformed

    private void jButtonAdicionaListaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAdicionaListaActionPerformed
        // TODO add your handling code here:
        nomeProduto =(String) jComboProdutos.getSelectedItem();
        System.out.println(nomeProduto);
        listaProdutos.addElement(nomeProduto);
        qtdProduto=  jTextVendaQuantidade.getText();
        Mercadoria p= new Mercadoria();
        p.setNomeItem(nomeProduto);
        p.setQtdItem(Integer.parseInt(qtdProduto));
        produtosVendidos.add(p);
        
        
    }//GEN-LAST:event_jButtonAdicionaListaActionPerformed
    
    public void carregaJList(){
        listaProdutos = new DefaultListModel();
        jListProdutos.setModel(listaProdutos);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaVenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new JanelaVenda().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(JanelaVenda.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton jButtonAdicionaLista;
    private javax.swing.JToggleButton jButtonVendaFinaliza;
    private javax.swing.JComboBox jComboNomeClientes;
    private javax.swing.JComboBox jComboProdutos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jListProdutos;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextValtotal;
    private javax.swing.JTextField jTextVendaQuantidade;
    // End of variables declaration//GEN-END:variables

     private void carregaComboBox(){
        ArrayList<String> nomes = carregaControle.getNomeClientes();
        jComboNomeClientes.removeAllItems();
        jComboNomeClientes.addItem("--Selecione Nome--");
        for(String c:nomes)
            jComboNomeClientes.addItem(c);
        jComboNomeClientes.setSelectedIndex(0);
       
    }
     
    private void carregaComboBox2(){
        ArrayList<String> nomes = carregaControle.getNomeProdutos();
         jComboProdutos.removeAllItems();
         jComboProdutos.addItem("--Selecione Nome--");
        for(String c:nomes)
             jComboProdutos.addItem(c);
         
         jComboProdutos.setSelectedIndex(0);
    }
   
    
}

